<?php

defined('_JEXEC') or die('Restricted access');
use Joomla\Registry\Registry;
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');

class ConsoleModelEvents extends JModelList
{
    /**store
     * @param $inputs
     * @return bool
     */
    public function store($inputs) {
        $inputs['appkey'] = self::getCode();
        $trackDate = JFactory::getDate()->format('Y-m-d H:i:s');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $user = JFactory::getUser();
        $query->insert('pp_event_cfg')
            ->columns(
                array(
                    $db->quoteName('app_id'), $db->quoteName('event_id'),
                    $db->quoteName('event_name'),$db->quoteName('comp_param_type'),
                    $db->quoteName('comp_param_name')
                )
            )
            ->values(' "'.$inputs['app_id'].'",  "'.$inputs['event_id'].'", "'.$inputs['event_name'].'", "'.$inputs['comp_param_type'].'", "'.$inputs['comp_param_name'].'" ' );

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    /**update
     * @param $inputs
     * @return bool
     */
    public function update($id,$inputs) {
//        $inputs['appkey'] = self::getCode();
//        $trackDate = JFactory::getDate()->format('Y-m-d H:i:s');
        $db = $this->getDbo();
        $user = JFactory::getUser();


        $query = $db->getQuery(true)
            ->update('pp_event_cfg')
            ->set("app_id = '".trim($inputs['app_id'])."',event_id = '".trim($inputs['event_id'])."',event_name = '".trim($inputs['event_name'])."',comp_param_type = '".trim($inputs['comp_param_type'])."',comp_param_name = '".trim($inputs['comp_param_name'])."'")
            ->where('id = ' . (int) $id);

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    protected function getListQuery()
    {
        $app = JFactory::getApplication('site');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $user = JFactory::getUser();
        $params = $app->getParams();
        $this->setState('params', $params);
        $query->select('ec.*,app.name')
        	->from('pp_event_cfg as ec')
            ->join('INNER', 'pp_app AS app ON app.id = ec.app_id')
            ->where('app.account_id='.$user->id)
        	->order('ec.event_name');
        $session = JFactory::getSession();
        $appId = $session->get('appId', -1);
        if ($appId > -1) {
        	$query->where('ec.app_id=' . $appId);
        }
        $query->order('ec.event_name');

        return $query;
    }

    public function getApp()
    {
        $user = JFactory::getUser();
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('pp_app')
            ->where('account_id='.$user->id);
        $query->order('id asc');
        $db->setQuery($query);
        $items = $this->_getList($query);
        return $items;
    }

    public function getItem($id = null)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('pp_event_cfg ')
            ->where('id='.$id);
        $db->setQuery($query);
        $Item= $db->loadObject();
        return $Item;
    }

    /**delete
     * @param $id
     */
    public function delete($id)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->delete('pp_event_cfg')

            ->where('id='.$id);

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    /**获取唯一字符串
     * @return string
     */
    protected static function getCode()
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }
}
